<template>
  <el-container>
    <el-header>
      <div class="title-block">
        <div class="title-box">
          <span style="font-size: 22px; color: #fff"
            >文件递交系统 <i class="el-icon-s-promotion"></i
          ></span>
        </div>
      </div>
      <ul class="exit-block">
        <li class="exit-item" @click="logout"><i class="el-icon-switch-button"></i> 注销</li>
      </ul>
    </el-header>
    <el-main>
      <div class="content-header">
        <el-row class="row">
          <el-col :span="24">
            <div class="header-section">
              <h1 class="inline">提交情况</h1>
              <h5 class="inline">
                -
                <a href="" id="task-name" @click.prevent=""> {{ this.task.title }}</a>
              </h5>
              <span id="task-status" class="label">{{ this.task.status }}</span>
              
            </div>
          </el-col>
        </el-row>
      </div>
      <div class="block full">
        <div class="block-title">
          <ul class="nav nav-tabs">
            <li :class="[isActive === 1 ? 'active' : 'unactive']">
              <a href="" @click.prevent="changeList(1)"
                ><i class="el-icon-document"></i>已提交名单</a
              >
            </li>
            <li :class="[isActive === 2 ? 'active' : 'unactive']">
              <a href="" @click.prevent="changeList(2)"
                ><i class="el-icon-document"></i>未提交名单</a
              >
            </li>
          </ul>
        </div>
        <div class="tab-content">
          <div class="block-tabs">
            <div class="block full">
              <div class="block-title">
                <h2 class="el-icon-document-checked">完成情况</h2>
              </div>
              <div class="block full">
                <div style="display: inline">
                  <a class="btn btn-primary" @click="zipDownload"
                    ><i class="el-icon-plus"></i>批量下载</a
                  >
                </div>
                <div
                  class="input-group"
                  style="float: right; width: 15%; margin-bottom: 1%"
                >
                  <input
                    type="text"
                    id="input-search-survey"
                    name="example-input1-group2"
                    class="form-control"
                    placeholder="搜   索"
                    style="text-align: center"
                  />
                  <span class="input-group-btn">
                    <a
                      class="btn btn-effect-ripple btn-primary"
                      style="overflow: hidden; position: relative"
                    >
                      <i class="el-icon-search"></i>
                    </a>
                  </span>
                </div>

                <el-table
                  ref="multipleTable"
                  :data="tableData"
                  tooltip-effect="dark"
                  style="width: 100%"
                  @selection-change="handleSelectionChange"
                >
                  <el-table-column type="selection" width="100">
                  </el-table-column>
                  <el-table-column prop="id" label="提交人id" width="200">
                  </el-table-column>
                  <el-table-column prop="name" label="提交人姓名" width="300">
                  </el-table-column>
                  <el-table-column
                    prop="word"
                    label="提交人文档"
                    width="300"
                    show-overflow-tooltip
                  >
                  </el-table-column>
                  <el-table-column
                    label="操作"
                    show-overflow-tooltip
                    width="400"
                  >
                    <template slot-scope="scope">
                      <a
                        @click.prevent="
                          fileDownload(
                            scope.row.word,
                            scope.row.name
                          )
                        "
                        class="btn btn-primary"
                        >下载</a
                      >
                      <a class="btn btn-danger">重置</a>
                      <a class="btn btn-primary" @click.prevent="fileView(scope.row.word)">在线预览</a>
                    </template>
                  </el-table-column>
                </el-table>

                <el-divider></el-divider>
                <div class="block" style="text-align: right">
                  <el-pagination
                    @current-change="getPageData"
                    :current-page="pageObject.pageNum"
                    :page-size="pageObject.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pageObject.totalPage"
                  >
                  </el-pagination>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { getTask, getSlist } from "@/api/admin.js";
import { formatDate } from "@/utils/dateformat.js";
import { downSubTaskFile,downZip,getFileUrl } from "@/api/file.js";

const pageParam = {
  list: [],
  pageNum: null,
  pageSize: null,
  total: null,
  totalPage: null,
};
export default {
  data() {
    return {
      tid: null,
      task: null,
      currentPage: 1,
      pageObject: null,
      tableData: [],
      isActive: 1,
    };
  },

  created() {
    this.pageObject = Object.assign({}, pageParam);
    this.tid = this.$route.query.tid;
    this.getTaskInfo();
    this.getPageData(1);
  },

  methods: {
    logout() {
      this.$store.dispatch("LogOut").then((response) => {});
      this.$router.push({
        path: "/login",
      });
    },

    fileView(word){
      let params = new URLSearchParams();

      params.append("name", word);
      
      getFileUrl(params).then(response=>{
        //let url = response.data + '&fullfilename=' + word + '.doc';
        let url = response.data;
        console.log(url);
        let Base64 = require('js-base64').Base64;
        window.open('http://8.136.202.110:8012/onlinePreview?url=' + encodeURIComponent(Base64.encode(url)));
      });
    },

    changeTitle(){
      this.$prompt('请输入新的任务标题', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          this.$message({
            type: 'success',
            message: '已将任务标题修改为: ' + value
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
    changeDate(){
      this.$prompt('请输入新的日期', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          this.$message({
            type: 'success',
            message: '已将任务截至日期修改为: ' + value
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
    startTask(){
      this.$prompt('请输入新的日期', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          this.$message({
            type: 'success',
            message: '已将任务截至日期修改为: ' + value
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
    changeList(index) {
      if (index === 1) {
        this.$router.push({
          path: "/adminTaskSlist",
          query: { tid: this.tid },
        });
      } else {
        this.$router.push({
          path: "/adminTaskDlist",
          query: { tid: this.tid },
        });
      }
    },
    getPageData(val) {
      this.tid = this.$route.query.tid;
      let params = new URLSearchParams();
      params.append("tid", this.tid);
      params.append("pageNum", val);
      getSlist(params).then((response) => {
        let data = response.data;
        this.pageObject.list = data.list;
        this.pageObject.pageNum = data.pageNum;
        this.pageObject.pageSize = data.pageSize;
        this.pageObject.total = data.total;
        this.pageObject.totalPage = data.totalPage;
        for (var i = 0; i < data.list.length; i++) {
          let item = {};
          item.id = data.list[i].submitter.id;
          item.name = data.list[i].submitter.name;
          item.word = data.list[i].words[0].word;
          this.tableData.push(item);
        }
      });
    },
    
    fileDownload(filename, name) {
      let params = new URLSearchParams();
      params.append("filename", filename);
      downSubTaskFile(params).then((response) => {
        const blob = new Blob([response], { type: "text/pain;charset=utf-8" });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement("a");
        a.download =
          this.task.title + "+" + name + "+" + formatDate(new Date()) + ".docx";
        a.href = url;
        a.click();
        window.URL.revokeObjectURL(url);
      });
    },
    getTaskInfo() {
      getTask(this.tid).then((response) => {
        let data = response.data;
        this.task = {};
        this.task.id = data.id;
        this.task.description = data.description;
        this.task.title = data.title;
        this.task.status = this.getTaskStatus(data.status);
        this.task.stime = data.createTime;
        this.task.etime = data.deadline;
      });
    },
    getTaskStatus(status) {
      var name = "";
      switch (status) {
        case 0:
          name = "未开始";
          break;
        case 1:
          name = "运行中";
          break;
        case 2:
          name = "已结束";
          break;
        case 3:
          name = "暂停中";
          break;
      }
      return name;
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    zipDownload() {
      if (this.multipleSelection.length === 0)
        this.$message.error("未选择提交人");
      else {
        var names = [];
        var ids = [];
        for (var i = 0; i < this.multipleSelection.length; i++) {
          names.push(this.multipleSelection[i].word);
          ids.push(this.multipleSelection[i].id);
        }
        let params = new URLSearchParams();
        params.append("names", names);
        downZip(params).then((response) => {
          const blob = new Blob([response], {
            type: "text/pain;charset=utf-8",
          });
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement("a");
          a.download = this.task.title + "_" + formatDate(new Date()) + ".zip";
          a.href = url;
          a.click();
          window.URL.revokeObjectURL(url);
        });
      }
    },
  },
};
</script>

<style scoped>
.el-header {
  width: 100%;
  background: #009688;
  position: fixed;
  z-index: 1030;
}
.exit-block {
  position: absolute;
  right: 10px;
  top: 0;
}

.exit-item {
  list-style: none;
  color: white;
  margin: 0 15px;
  line-height: 30px;
}

.title-box {
  left: 0;
  width: 300px;
  line-height: 60px;
}

.title-block {
  align-items: center;
  width: 185px;
  height: 70px;
}

.el-main {
  min-height: 472px;
  padding: 20px 20px 1px;
  padding-top: 80px;
  background-color: #eeebf2;
}

.content-header {
  background-color: #ffffff;
  border-bottom: 2px solid #dae0e8;
  border-bottom-color: rgb(218, 224, 232);
  margin: -10px -5px 10px;
  padding: 0 15px;
  margin: -20px -20px 20px;
  border-bottom-color: #dfdae8;
  padding-left: 20px;
  padding-right: 20px;
}

.row {
  margin-left: -15px;
  margin-right: -15px;
}

.header-section {
  padding: 15px 0px;
}

.content-header h1,
h2 {
  margin: 5px 0;
  line-height: 28px;
}

.content-header h1 {
  font-weight: 500;
  font-size: 24px;
}

.label {
  display: inline;
  padding: 0.2em 0.6em 0.3em;
  font-size: 75%;
  font-weight: bold;
  line-height: 1;
  color: #fff;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25em;
  font-weight: normal;
  font-size: 90%;
  padding: 1px 4px;
  margin-left: 40px;
  color: black;
}

.block-title .nav-tabs {
  margin: 0;
  padding: 0;
  border-bottom: 0;
}

.nav-tabs > li {
  float: left;
  margin-bottom: 0;
  position: relative;
  display: block;
}

li.active > a {
  background-color: #dfdae8;
}

li > a {
  color: #353a4d;
  border: none;
  min-height: 40px;
  line-height: 40px;
  padding-top: 0;
  padding-bottom: 0;
  margin: 0;
  border-radius: 0;
  padding-left: 15px;
  padding-right: 15px;
  font-weight: 500;
  position: relative;
  display: block;
  text-decoration: none;
}

.block-title {
  height: 40px;
}

.input-group {
  margin-top: 1px;
  position: relative;
  display: table;
  border-collapse: separate;
}

.input-group .form-control {
  position: relative;
  z-index: 2;
  float: left;
  margin-bottom: 0;
  border-color: #dfdae8;
}

.form-control {
  padding: 6px 8px;
  max-width: 100%;
  margin: 1px 0;
  border-radius: 3px;
  transition: border-color ease-in-out 0.15s, box-shadow ease-in-out 0.15s;
  border: 1px solid #ccc;
  background-color: #fff;
  background-image: none;
  height: 20px;
}


</style>